package leeon.forpeddy;

import ipworks.IPWorksException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TooManyListenersException;

import leeon.forpeddy.download.HtmlDownloader;
import leeon.forpeddy.excel.StudentExcelWrapper;
import leeon.forpeddy.htmlparse.StudentHtmlParser;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StudentHelper {
	private static Log logger = LogFactory.getLog(StudentHelper.class);
	
	public static final String BASE_DIR = "D:\\workspace\\forpeddy\\misc\\";
	public static final String HTTP_URL = "http://218.1.69.105/acad/student/exportExcel.jsp";
	//public static final String HTTP_URL = "http://218.1.69.105/gqb/student/exportExcel_luan.jsp";
	//public static final String HTTP_URL = "http://218.1.69.105/gsz/student/exportExcel.jsp";
			
	private List<Map<String, String>> list = null;
	private List<Map<String, String>> result = null;
	
	public StudentHelper() {
		StudentExcelWrapper sew = null;
		try {
			sew = new StudentExcelWrapper(new File(BASE_DIR + "list.xls"));
			this.list = sew.readAll();
		} catch (Exception e) {
			logger.error("something wrong...", e);
		} finally {
			try {
				sew.save();
			} catch (Exception e) {
				logger.error("something wrong...", e);
			}
		}
		result = new ArrayList<Map<String, String>>();
	}
	
	public void doStartDownload() throws IPWorksException, TooManyListenersException, IOException {
		HtmlDownloader h = new HtmlDownloader();
		for (Map<String, String> row : this.list) {
			String fileName = row.get("code") + "_" + row.get("name");
			File file = new File(BASE_DIR + fileName + ".html");
			if (!file.exists() || file.length() == 0) {
				h.start(HTTP_URL + "?classNo=" + row.get("code"));
				h.saveToFile(file);
			} else {
				logger.info("download html for student[" + row.get("code") + "] " + fileName + " file exists");
			}
		}
		h.destroy();
	}
	
	public void doOutput() {
		for (Map<String, String> r : this.result) {
			String sql = "insert into student (no, name, sex, birthday, folk, mobile, address, email, idcard, cet, class) values ('" +
					r.get("no") +
					"', '" +
					r.get("name") +
					"', '" +
					r.get("sex") +
					"', to_date('" +
					r.get("birthday") +
					"','yyyy-mm-dd'), '" +
					r.get("folk") +
					"', '" +
					r.get("mobile") +
					"', '" +
					r.get("address") +
					"', '" +
					r.get("email") +
					"', '" +
					r.get("idcard") +
					"', '" +
					r.get("cet") +
					"', '" +
					r.get("class") +
					"');";
			logger.info(sql);
		}
	}


	public void doParseHtml() throws IOException {
		for (Map<String, String> row : this.list) {
			String fileName = row.get("code") + "_" + row.get("name");
			logger.info("parser html for class[" + row.get("code") + "] " + fileName);
			StudentHtmlParser chp = new StudentHtmlParser(BASE_DIR + fileName + ".html");
			result.addAll(chp.getResults(row.get("code")));
		}
	}

	
	public static void main(String[] args) {
		StudentHelper sh = null;
		try {
			sh = new StudentHelper();
//			sh.doStartDownload();
			sh.doParseHtml();
			sh.doOutput();
		} catch (Exception e) {
			logger.error("something wrong...", e);
		}

	}

}
